package com.learn.project2.service.wx.collect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketCollect;
import com.learn.project2.bean.MarketCollectExample;
import com.learn.project2.bean.MarketGoods;
import com.learn.project2.bean.MarketUser;
import com.learn.project2.mapper.MarketCollectMapper;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.mapper.MarketUserMapper;
import com.learn.project2.model.bo.wx.collect.CollectAddOrDeleteBO2;
import com.learn.project2.model.bo.wx.collect.CollectListBO2;
import com.learn.project2.model.vo.collect.CollectListVO2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 亓永杰
 * @version 1.0
 * @description: 收藏
 * @date 2022-01-09 23:16
 */
@Service
public class CollectServiceImpl2 implements CollectService2 {

    @Autowired
    MarketCollectMapper collectMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketUserMapper userMapper;

    /**
     * @description: 获取收藏夹
     * @param:
     * @return:
     * @author 亓永杰
     * @date: 2022-01-10 22:24
     */
    @Override
    public CollectListVO2 collectList(CollectListBO2 collectListBO2) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.setOrderByClause("update_time desc");
        collectExample.createCriteria().andUserIdEqualTo(user.getId()).andTypeEqualTo(collectListBO2.getType()).andDeletedEqualTo(false);
        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);
        ArrayList<CollectListVO2.ListBean> list = new ArrayList<>();
        for (MarketCollect collect : collectList) {
            MarketGoods goods = goodsMapper.selectByPrimaryKey(collect.getValueId());
            CollectListVO2.ListBean listBean = new CollectListVO2.ListBean(goods.getBrief(), goods.getPicUrl(), goods.getId(), goods.getName(), collect.getId(), collect.getType(), goods.getRetailPrice());
            list.add(listBean);
        }
        PageHelper.startPage(collectListBO2.getPage(), collectListBO2.getLimit());
        CollectListVO2 collectListVO2 = new CollectListVO2();
        PageInfo<CollectListVO2.ListBean> pageInfo = new PageInfo<>(list);
        collectListVO2.setLimit(collectListBO2.getLimit());
        collectListVO2.setList(pageInfo.getList());
        collectListVO2.setPage(collectListBO2.getPage());
        collectListVO2.setPages(pageInfo.getPages());
        collectListVO2.setTotal(pageInfo.getTotal());
        return collectListVO2;
    }

    /**
     * @description: 添加收藏或取消收藏
     * @param:
     * @return:
     * @author 亓永杰
     * @date: 2022-01-10 22:46
     */
    @Override
    public void collectAddOrDelete(CollectAddOrDeleteBO2 collectAddOrDeleteBO2) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();

        MarketCollectExample collectExample = new MarketCollectExample();
        //通过物品iD和typeId，还有userId查找用户是否收藏此商品（为null则添加）
        collectExample.createCriteria().andValueIdEqualTo(collectAddOrDeleteBO2.getValueId()).andTypeEqualTo(collectAddOrDeleteBO2.getType()).andUserIdEqualTo(user.getId());
        List<MarketCollect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() != 0) {
            MarketCollect collect = collects.get(0);
            collect.setDeleted(!collect.getDeleted());
            collect.setUpdateTime(new Date());
            collectMapper.updateByPrimaryKeySelective(collect);
        } else {
            MarketCollect collect = new MarketCollect();
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collect.setType(collectAddOrDeleteBO2.getType());
            collect.setDeleted(false);
            collect.setUserId(user.getId());
            collect.setValueId(collectAddOrDeleteBO2.getValueId());
            collectMapper.insertSelective(collect);
        }


    }


}
